package cn.chengpengper.netdisk.controller;

import cn.chengpengper.netdisk.config.MinioConfig;
import cn.chengpengper.netdisk.util.CommonUtil;
import cn.chengpengper.netdisk.util.JsonData;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
@RestController
@RequestMapping("/api/test/v1")
public class TestController {


    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioClient client;


    @PostMapping("/upload")
    public JsonData testUpload(@RequestParam("file") MultipartFile file){

        String fileName= file.getOriginalFilename();


        // 读取文件流 上传到minio
        try {
            InputStream inputStream = file.getInputStream();

            client.putObject(PutObjectArgs.builder().bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .stream(inputStream, file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        String URL = minioConfig.getEndpoint() + "/" + minioConfig.getBucketName() + "/" + fileName;
        return JsonData.buildSuccess(URL);

    }
}
